<?php

/**
 * 访问量统计
**/

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Tickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('流量看板');
        $this->height(400);
        $this->chartHeight(300);
        $this->chartLabels('今日访问占比');
        $this->dropdown([
            'day'=>'天访问量',
            '7' => '周访问量',
            '30' => '月访问量',
            '365' => '年访问量',
            'all' =>'总访问量',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $pv = DB::table('visit_log')->whereDate('date',date('Y-m-d'))->count();
        $ones = DB::table('visit_log')
            ->select(DB::raw('count(*) as pv,province'))
            ->groupBy('province')
            ->orderBy('pv','desc')
            ->get();
        $info = [];
        foreach ($ones as $one){
            $info[] = $one->province." ".$one->pv;
        }
        $all = DB::table('visit_log')->count();//总访问量
        $gdp = $pv / $all;
        $gdp = $gdp * 100;
        switch ($request->get('option')) {
            case 'all':
                //访问量
                $pv = DB::table('visit_log')->count();
                // 卡片内容
                $this->withContent($pv);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
                // 图表数据
                $this->withChart($gdp);
                break;
            case '365':
                $start_time = date("Y-01-01");
                $end_time = date('Y-12-31');
                //访问量
                $pv = DB::table('visit_log')
                    ->whereDate('date','>=',$start_time)
                    ->whereDate('date','<=',$end_time)->count();
                // 卡片内容
                $this->withContent($pv);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
                // 图表数据
                $this->withChart($gdp);
                break;
            case '30':
                $start_time = date("Y-m-01");
                $end_time = date('Y-m-31');
                //访问量
                $pv = DB::table('visit_log')
                    ->whereDate('date','>=',$start_time)
                    ->whereDate('date','<=',$end_time)->count();
                // 卡片内容
                $this->withContent($pv);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
                // 图表数据
                $this->withChart($gdp);
                break;
            case '7':
                $start_time = date("Y-m-d",strtotime('-7 day'));
                $end_time = date('Y-m-d');
                //访问量
                $pv = DB::table('visit_log')
                    ->whereDate('date','>=',$start_time)
                    ->whereDate('date','<=',$end_time)->count();
                // 卡片内容
                $this->withContent($pv);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
                // 图表数据
                $this->withChart($gdp);
                break;
            case 'day':
                $start_time = date("Y-m-d");
                $end_time = date('Y-m-d');
                //访问量
                $pv = DB::table('visit_log')
                    ->whereDate('date','>=',$start_time)
                    ->whereDate('date','<=',$end_time)->count();
                // 卡片内容
                $this->withContent($pv);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
                // 图表数据
                $this->withChart($gdp);
                break;
            default:
                // 卡片内容
                $this->withContent($pv);
                // 图表数据
                $this->withChart($gdp);
                // 卡片底部
                $this->withFooter($info[0], $info[1], $info[2]);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <small>访问量</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>访问TOP1</p>
        <span >{$new}</span>
    </div>
    <div class="text-center">
        <p>访问TOP2</p>
        <span >{$open}</span>
    </div>
    <div class="text-center">
        <p>访问TOP3</p>
        <span >{$response}</span>
    </div>
</div>
HTML
        );
    }
}
